<!doctype html>
<html>

	<head>
		<meta charset="UTF-8">
		<title>收货地址管理</title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">
		<link href="/weixin/css/mui.min.css" rel="stylesheet" />
		<style type="text/css">
			.lmj-passenger-cell-div{
			    background-color: #FFFFFF;
			    padding-top: 2px;
			    padding-bottom: 1px;
			    margin-bottom: 10px;
			}
			.lmj-table-cell-vcenter{
				vertical-align: middle;
				font-size: 15px;
			}
			.mui-table-cell>button{
				padding: 1px;
				width: 50px;
				margin-left: 12px;
			}
			.lmj-hr{
				border-top:1px solid; 
				height:0px; 
				border-right:0px; 
				border-bottom:0px; 
				border-left:0px;
				border-color:rgba(0,0,0,0.1);
			}
			.radio-check {
				width: 18px;
				height: 18px;
				float: left;
				margin-top: 1px;
				cursor: pointer;
				text-align: center;
				margin-right: 4px;
				background-image: url(images/check.png);
				background-size: 18px;
				background-repeat: no-repeat;
				background-position: -24px 0;
				background-position: 0 0;
			}
			.radio-no-check {
				width: 18px;
				height: 18px;
				float: left;
				margin-top: 1px;
				cursor: pointer;
				text-align: center;
				margin-right: 4px;
				background-image: url(images/on-check.png);
				background-size: 18px;
				background-repeat: no-repeat;
				background-position: -24px 0;
				background-position: 0 0;
			}
			.radioclass {
				opacity: 0;
				cursor: pointer;
				-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";
				filter: alpha(opacity=0);
			}
		</style>
	</head>

	<body>
		<header class="mui-bar mui-bar-nav">
		    <a class="mui-action-back mui-icon mui-icon-back mui-pull-left"></a>
		    <h1 class="mui-title">收货地址管理</h1>
		</header>
		<div class="mui-content" id="receiptAddrDiv">
			<button type="button" class="mui-btn mui-btn-block lmj-add-addr-btn"><span class="mui-icon mui-icon-plusempty"></span>新增地址</button>
			
		</div>
		<script src="/weixin/js/mui.min.js"></script>
		<script src="/weixin/js/jquery.min.js"></script>
		<script src="/weixin/js/comment.js"></script>
		<script type="text/javascript">
			mui.init();
			
			mui.ready(function(){
				var btnArray = ['否', '是'];
				
				//获取收货地址列表
				setTimeout(function(){
					jQuery.ajax({
						type:'get',
						url:ajaxUrlPath+'/wx/address/show',
						async:true,
						success:function(data){
							for(var i=0;i<data.length;i++){
								var receiptAddrCell="<div class='lmj-passenger-cell-div'><div class='mui-content-padded mui-table'><div class='mui-table-cell mui-col-xs-7 lmj-table-cell-vcenter'>";
								if(data[i].isDeleted==2){
									//默认地址
									receiptAddrCell=receiptAddrCell+"<label><span class='radio-check'><input type='radio' checked name='defaultAddrRadio' class='default-addr-radio radioclass' data-id='"+data[i].id+"'></span>设为默认收货地址</label>";
								}else{
									receiptAddrCell=receiptAddrCell+"<label><span class='radio-no-check'><input type='radio' name='defaultAddrRadio' class='default-addr-radio radioclass' data-id='"+data[i].id+"'></span>设为默认收货地址</label>";
								}
								receiptAddrCell=receiptAddrCell+"</div><div class='mui-table-cell mui-col-xs-5'>"+
								"<button type='button' class='mui-btn lmj-edit-addr-btn' data-id='"+data[i].id+"'><span class='mui-icon mui-icon-compose'></span>编辑</button>"+
								"<button type='button' class='mui-btn lmj-delete-addr-btn' data-id='"+data[i].id+"'><span class='mui-icon mui-icon-closeempty'></span>删除</button></div></div>";
								receiptAddrCell=receiptAddrCell+"<hr class='lmj-hr'><div class='mui-content-padded'><h4>"+data[i].name+"&nbsp;&nbsp;&nbsp;&nbsp;"+data[i].mobile+"</h4><p>"+data[i].address+""+data[i].cityDetail+"</p></div></div>"
								jQuery("#receiptAddrDiv").append(receiptAddrCell);
							}
							//设为默认地址
							setDefalutAddr();
							//删除地址
							deleteDefaultAddr();
							//修改操作
							editAddr();
						},
						error:function(){
							//异常处理；
							console.log(type);
						}
					});
				},200);
				
				
				//设置默认地址
				function setDefalutAddr(){
					jQuery(".default-addr-radio").bind('click',function(){
						if(!jQuery(this).is('checked')){
							//点击的按钮没有选中
							var defaultAttrId=jQuery(this).attr('data-id');
							mui.confirm('是否修改默认收货地址？', '美丽乡村', btnArray, function(e) {
								if (e.index == 1) {
									//确定修改默认收货地址
									jQuery.ajax({
										type:'get',
										url:ajaxUrlPath+'/wx/address/default',
										data:{
											id:defaultAttrId
										},
										async:true,
										success:function(data){
											mui.alert('修改成功', '美丽乡村', function() {
												window.location.reload();
											});
										},
										error:function(){
											mui.alert('网络不稳定，请稍后再试...', '美丽乡村', function() {
												window.location.reload();
											});
										}
									});
								}
							})
						}
					});
				}
				
				
				//修改默认收货信息
				function  editAddr(){
					jQuery(".lmj-edit-addr-btn").bind("tap",function(){
						var addressId=jQuery(this).attr("data-id");
						window.location='add-receipt-address.html?addressId='+addressId;
					});
				}
				
				//新增旅客信息
				jQuery(".lmj-add-addr-btn").bind("tap",function(){
					window.location='add-receipt-address.html?addressId=';
				});
				
				//删除按钮
				function deleteDefaultAddr(){
					jQuery(".lmj-delete-addr-btn").bind("tap",function(){
						var receiptAddrId=jQuery(this).attr('data-id');
						mui.confirm('确定删除该收货地址？', '美丽乡村', btnArray, function(e) {
							if (e.index == 1) {
								//确定删除
								jQuery.ajax({
									url:ajaxUrlPath+'/wx/address/delete',
									data:{
										id:receiptAddrId
									},
									type:'get',
									success:function(data){
										mui.alert('删除成功', '美丽乡村', function() {
											window.location.reload();
										});
									},
									error:function(){
										mui.alert('网络不稳定，请稍后再试...', '美丽乡村', function() {
											window.location.reload();
										});
									}
								});
							} else {
								//未删除
							}
						})
					});
				}
			
			});
		</script>
	</body>

</html>